package com.xzw.cameratest.organization;

import android.text.TextUtils;
import android.util.Log;

import com.android.business.entity.ChannelInfo;
import com.android.business.entity.GroupInfo;
import com.android.business.exception.BusinessException;
import com.xzw.cameratest.MainActivity;
import com.xzw.cameratest.bean.Node;
import com.xzw.cameratest.common.Config;
import com.xzw.cameratest.utils.SPUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @descripe:
 * @author:111144
 * @date:2020/4/27
 */
public class GroupLoadManager {

    private List<Node> mAllNodes = new ArrayList<>();

    static class Instance {
        public static final GroupLoadManager instance = new GroupLoadManager();
    }

    public static GroupLoadManager getInstance() {
        return Instance.instance;
    }

    private GroupLoadManager() {
    }

    public void loadGroup() throws BusinessException {
        String groupId = SPUtils.getString(Config.GROUP_ID, "");
        List<GroupInfo> groupInfoList = GroupAdapterImpl.getInstance().loadGroup(groupId);
        mAllNodes.clear();
        convertGroup2Node(getRootNode(groupInfoList), groupInfoList);
    }

    /**
     * 从组织列表中获取根节点
     *
     * @param groupInfoList
     * @return
     */
    private Node getRootNode(List<GroupInfo> groupInfoList) {
        GroupInfo root = null;
        for (GroupInfo groupInfo : groupInfoList) {
            if (TextUtils.isEmpty(groupInfo.getGroupParentId())) {
                root = groupInfo;
                break;
            }
        }

        if (root == null) {
            root = groupInfoList.get(0);
        }

        groupInfoList.remove(root);
        return new Node(root, root.getGroupParentId(), root.getGroupId());
    }

    /**
     * 将groupInfo转换为Node节点插入 mAllNodes集合
     *
     * @param node
     * @param groupInfoList
     */
    private void convertGroup2Node(Node node, List<GroupInfo> groupInfoList) {
        mAllNodes.add(node);
        Iterator<GroupInfo> iterator = groupInfoList.iterator();
        while (iterator.hasNext()) {
            GroupInfo groupInfo = iterator.next();
            if (node.getGroupId().equals(groupInfo.getGroupParentId())) {
                Node childNode = new Node(groupInfo, groupInfo.getGroupParentId(), groupInfo.getGroupId());
                childNode.setParent(node);
                node.getChildNodes().add(childNode);
                iterator.remove();
            }
        }

        List<Node> childNodes = node.getChildNodes();
        if (childNodes != null) {
            for (Node childNode : childNodes) {
                convertGroup2Node(childNode, groupInfoList);
            }
        }

    }

    public void loadChannel(Node parentNode, List<String> channelIdList) throws BusinessException {
        Log.e("GroupLoadManager", "==================97=========================");

        List<ChannelInfo> channelInfoList = loadChannelByPlatform(parentNode, channelIdList);
//        MainActivity.onR1.getMsg();
        Log.e("GroupLoadManager", "==================101=========================");

        List<ChannelInfo> filterResult = filterPlayRight(channelInfoList);
        Log.e("GroupLoadManager", "==================104=========================");

        convertChannel2Node(parentNode, filterResult);
        Log.e("GroupLoadManager", "==================107=========================");

    }

    /**
     * 根据dssPlatform 判断加载方式
     * dssPlatform 1:CLOUD;2:MOBILE;3:GDSS
     * CLOUD 环境需要通过组织id 加载通道
     * MOBILE 和 GDSS 则使用需要查询的通道id 去加载通道
     *
     * @param parentNode
     * @param channelIdList
     * @return
     * @throws BusinessException
     */
    private List<ChannelInfo> loadChannelByPlatform(Node parentNode, List<String> channelIdList) throws BusinessException {
        List<ChannelInfo> channelInfoList = new ArrayList<>();
//        if (BuildConfig.dssPlatform == 1){
//            channelInfoList = GroupAdapterImpl.getInstance().loadChannelIdByGroupId(parentNode.getGroupId());
//        }

//        else {


        if (channelIdList != null) {
            channelInfoList = GroupAdapterImpl.getInstance().loadChannelByChannelIds(channelIdList);
        }
//        }

        return channelInfoList;
    }

    /**
     * 判断权限需要使用权限常量和rights字段进行&运算结果为 1 则有权限否则无权限
     * <p>
     * 判断当前通道是否有实时播放权限
     * 根据实时预览权限过滤组织树
     *
     * @param channelInfoList
     * @return
     */
    private List<ChannelInfo> filterPlayRight(List<ChannelInfo> channelInfoList) {
        List<ChannelInfo> result = new ArrayList<>();
        for (ChannelInfo channelInfo : channelInfoList) {
            if ((channelInfo.getRights() & ChannelInfo.Rights.REAL_MONITOR) == 1) {
                result.add(channelInfo);
            }
        }
        return result;
    }

    /**
     * 将ChannelInfo 转换为Node 插入 mAllNodes 集合中
     *
     * @param parentNode
     * @param channelInfoList
     */
    private void convertChannel2Node(Node parentNode, List<ChannelInfo> channelInfoList) {

        List<Node> nodes = new ArrayList<>();
        for (ChannelInfo channelInfo : channelInfoList) {
            Node node = new Node(channelInfo, parentNode.getGroupId(), channelInfo.getGroupUuId());
            node.setParent(parentNode);
            nodes.add(node);
        }

        int i = mAllNodes.indexOf(getLastChildNode(parentNode));
        mAllNodes.addAll(i + 1, nodes);
        parentNode.getChildNodes().addAll(nodes);
        parentNode.setLoadChannel(true);

        MainActivity.onR.getMsg(channelInfoList);

    }

    public Node getLastChildNode(Node parentNode) {
        if (parentNode.getChildNodes().isEmpty()) {
            return parentNode;
        }
        return getLastChildNode(parentNode.getChildNodes().get(parentNode.getChildNodes().size() - 1));
    }

    /**
     * 获取所有节点
     *
     * @return
     */
    public List<Node> getAllNodes() {
        return mAllNodes;
    }
}
